package com.zretc.controller;

import com.github.pagehelper.PageInfo;
import com.zretc.entity.Manager;
import com.zretc.service.ManagerLoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 何亮龙
 * 后端管理员登录控制器
 */
@Controller
@RequestMapping("back")
public class BackManagerLoginController {
    private static final Logger logger = LoggerFactory.getLogger(BackManagerLoginController.class);
    @Autowired
    private ManagerLoginService managerLoginService;

    // 登录判断
    @PostMapping("/login")
    @ResponseBody
    public boolean adminLogin(HttpSession session,
                              String adminName,
                              String adminPwd,
                              String code
    ) {

        //进行验证码验证
        String sessionCode = session.getAttribute("code").toString().toLowerCase();
        if (code.equals(sessionCode)) {
            // 调用业务层
            Manager adminAccount = managerLoginService.adminLogin(adminName, adminPwd);
            // 判断是否为空
            if (adminName == null) {
                return false;
            } else {
                session.setAttribute("adminName", adminName);
                return true;
            }
        }

        return true;
    }

    @GetMapping("loginout")
    public String loginOut(HttpSession session) {
        session.setMaxInactiveInterval(0);
        return "redirect:views/managerLogin.jsp";
    }

    // 获取用户列表
    @GetMapping("/manager")
    @ResponseBody
    public PageInfo<Manager> getAccountsList(String adminId,
                                             String adminName,
                                             String adminPwd,
                                             String adminRole,
                                             String adminRegTime,
                                             @RequestParam("page") String pageNum,
                                             @RequestParam("limit") String pageSize) {
        // 组装条件
        Map<String, String> condition = new HashMap<>();
        condition.put("adminId", adminId);
        condition.put("adminName", adminName);
        condition.put("adminPwd", adminPwd);
        condition.put("adminRole", adminRole);
        condition.put("adminRegTime", adminRegTime);
        condition.put("pageNum", pageNum);
        condition.put("pageSize", pageSize);
        // 调用业务层
        PageInfo<Manager> pageInfo = managerLoginService.getManagerByCondition(condition);
        // 返回json数据
        return pageInfo;
    }
//添加管理员
    @PostMapping("/insertMnager")
    @ResponseBody
    public boolean insertManager(@RequestParam("adminName") String adminName,
                                @RequestParam("adminPwd") String adminPwd,
                                @RequestParam("adminRole") String adminRole,
                                @RequestParam("adminRegTime") String adminRegTime) {
        Manager manager = new Manager(null, adminName, adminPwd, adminRole, adminRegTime);
        return managerLoginService.addManager(manager);
    }
    //根据adminId删除单条管理员信息
    @DeleteMapping("/deleteManager/{adminId}")
    @ResponseBody
    public boolean deleteFilmByFilmId(@PathVariable("adminId") String adminId) {
        Integer a = Integer.valueOf(adminId);
        return managerLoginService.deleteByAdminId(a);
    }
}
